package com.lai;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DiceGameFrame extends JFrame{
    private int flag = 0;//用来标识玩家未押注，压大或压小的三种情况,0表示未押注，1表示押小，2表示押大
    private boolean ok = false;//标识游戏开始和结束
    private JLabel dice1 = new JLabel();//第一个骰子的标签
    private JLabel dice2 = new JLabel();//第二个骰子的标签
    private JLabel dice3 = new JLabel();//第三个骰子的标签
    private JButton btnBig = null;//"大"的按钮
    private JButton btnSmall = null;//"小"的按钮
    private JButton btnStart = null;//"开始"的按钮
    private JButton btnEnd = null;//"结束"的按钮
    private Thread startThread = null;//开始的线程
    public DiceGameFrame(){
        super();
        setTitle("一起来玩骰子吗？");
        Container con = getContentPane();
        con.setLayout(null);
        btnBig = new JButton();
        btnBig.setText("大");
        btnBig.setForeground(Color.BLACK);
        btnBig.setBackground(Color.LIGHT_GRAY);
        btnBig.setBounds(200,100,100,40);
        btnBig.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                flag = 2;
                btnBig.setBackground(Color.BLUE);
                btnBig.setForeground(Color.GREEN);
                System.out.println("玩家压大");
            }
        });
        con.add(btnBig);
        btnSmall = new JButton();
        btnSmall.setText("小");
        btnSmall.setForeground(Color.BLACK);
        btnSmall.setBackground(Color.LIGHT_GRAY);
        btnSmall.setBounds(520,100,100,40);
        btnSmall.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                flag = 1;
                System.out.println("玩家压小");
                btnSmall.setBackground(Color.BLUE);
                btnSmall.setForeground(Color.GREEN);
            }
        });
        con.add(btnSmall);
        ImageIcon image = new ImageIcon("img/dice1.png");
        dice1.setIcon(image);
        dice1.setBounds(200,185,120,120);
        con.add(dice1);
        dice2.setIcon(image);
        dice2.setBounds(350,185,120,120);
        con.add(dice2);
        dice3.setIcon(image);
        dice3.setBounds(500,185,110,110);
        con.add(dice3);
        btnStart = new JButton();
        btnStart.setText("开始");
        btnStart.setForeground(Color.BLACK);
        btnStart.setBackground(Color.LIGHT_GRAY);
        btnStart.setBounds(200,350,100,40);
        btnStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //当用户点击开始时，启动摇骰子的线程和判断计算骰子点数的线程
                if(flag!=0){
                    btnStart.setBackground(Color.RED);
                    btnStart.setForeground(Color.BLUE);
                    ok = true;
                    startThread = new Thread(new DiceThread());
                    startThread.start();
                    System.out.println("开始摇骰子~");
                }else{
                    JOptionPane.showMessageDialog(null,"请先下注！！！");
                }
            }
        });
        con.add(btnStart);
        btnEnd = new JButton();
        btnEnd.setText("结束");
        btnEnd.setForeground(Color.BLACK);
        btnEnd.setBackground(Color.LIGHT_GRAY);
        btnEnd.setBounds(520,350,100,40);
        btnEnd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //当用户点击结束时，结束摇骰子的线程并弹窗告诉玩家骰子总点数以及输赢
                btnEnd.setBackground(Color.RED);
                btnEnd.setForeground(Color.BLUE);
                ok = false;
                System.out.println("结束摇骰子~");
            }
        });
        con.add(btnEnd);
        setVisible(true);
        setBounds(200,100,820,540);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private class DiceThread implements Runnable{
        private int v1;//第一个骰子的点数
        private int v2;//第二个骰子的点数
        private int v3;//第三个骰子的点数
        private int total;//总骰子的点数
        @Override
        public void run() {
            while(true){
                if(ok){
                    v1 = (int)(Math.random()*6+1);
                    v2 = (int)(Math.random()*6+1);
                    v3 = (int)(Math.random()*6+1);
                    total = v1+v2+v3;
                    dice1.setIcon(new ImageIcon("img/dice"+v1+".png"));
                    dice2.setIcon(new ImageIcon("img/dice"+v2+".png"));
                    dice3.setIcon(new ImageIcon("img/dice"+v3+".png"));
                }else{
                    if(flag==2){
                        if(total>9){
                            JOptionPane.showMessageDialog(null,"点数是"
                            +total+"! 大!\n玩家赢！\n恭喜你~~~");
                        }
                        else{
                            JOptionPane.showMessageDialog(null,"点数是"
                                    +total+"!  小!\n玩家输！\n再接再厉喔~~~");
                        }
                    }else if(flag==1){
                        if(total>9){
                            JOptionPane.showMessageDialog(null,"点数是"
                                    +total+"!  大!\n玩家输！\n再接再厉喔~~~");
                        }
                        else{
                            JOptionPane.showMessageDialog(null,"点数是"
                                    +total+"!  小!\n玩家赢！\n恭喜你~~~");
                        }
                    }
                    flag = 0;
                    btnBig.setForeground(Color.BLACK);
                    btnBig.setBackground(Color.LIGHT_GRAY);
                    btnSmall.setForeground(Color.BLACK);
                    btnSmall.setBackground(Color.LIGHT_GRAY);
                    btnStart.setForeground(Color.BLACK);
                    btnStart.setBackground(Color.LIGHT_GRAY);
                    btnEnd.setForeground(Color.BLACK);
                    btnEnd.setBackground(Color.LIGHT_GRAY);
                    break;
                }
                try{
                    Thread.sleep(120);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
